package com.vj.router.server.impl;

import com.vj.router.common.*;
import com.vj.router.server.util.DeviceGatewayMapCacheUtil;
import io.grpc.stub.StreamObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class RouterDeviceGatewayServiceImpl extends RouterDeviceGatewayServiceGrpc.RouterDeviceGatewayServiceImplBase {

    private static final Logger logger = LoggerFactory.getLogger(RouterDeviceGatewayServiceImpl.class);

    @Override
    public void registerRoute(RegisterDeviceGatewayRouteRequest request, StreamObserver<RegisterDeviceGatewayRouteResponse> responseObserver) {
        String deviceId = request.getDeviceId();
        String gatewayId = request.getGatewayId();

        try {
            logger.info("Registering route for device: {} with gateway: {}", deviceId, gatewayId);
            DeviceGatewayMapCacheUtil.putDeviceIdToGatewayId(deviceId, gatewayId);

            RegisterDeviceGatewayRouteResponse response = RegisterDeviceGatewayRouteResponse.newBuilder()
                    .setMessage("Route registered successfully for device: " + deviceId)
                    .build();

            responseObserver.onNext(response);
            responseObserver.onCompleted();
        } catch (Exception e) {
            logger.error("Error registering route for device: " + deviceId, e);
            RegisterDeviceGatewayRouteResponse errorResponse = RegisterDeviceGatewayRouteResponse.newBuilder()
                    .setMessage("Error registering route: " + e.getMessage())
                    .build();
            responseObserver.onError(e);
        }
    }

    @Override
    public void getRoute(GetDeviceGatewayRouteRequest request, StreamObserver<GetDeviceGatewayRouteResponse> responseObserver) {
        String deviceId = request.getDeviceId();

        try {
            logger.info("Getting route for device: {}", deviceId);
            String gatewayId = DeviceGatewayMapCacheUtil.getDeviceIdToGatewayId(deviceId);

            GetDeviceGatewayRouteResponse response = GetDeviceGatewayRouteResponse.newBuilder()
                    .setGatewayId(gatewayId)
                    .setMessage("Route found for device: " + deviceId)
                    .build();

            responseObserver.onNext(response);
            responseObserver.onCompleted();
        } catch (Exception e) {
            logger.error("Error getting route for device: " + deviceId, e);
            GetDeviceGatewayRouteResponse errorResponse = GetDeviceGatewayRouteResponse.newBuilder()
                    .setMessage("Error getting route: " + e.getMessage())
                    .build();
            responseObserver.onError(e);
        }
    }

    @Override
    public void unregisterRoute(UnregisterDeviceGatewayRouteRequest request, StreamObserver<UnregisterDeviceGatewayRouteResponse> responseObserver) {
        String deviceId = request.getDeviceId();

        try {
            logger.info("Unregistering route for device: {}", deviceId);
            DeviceGatewayMapCacheUtil.removeDeviceIdToGatewayId(deviceId);

            UnregisterDeviceGatewayRouteResponse response = UnregisterDeviceGatewayRouteResponse.newBuilder()
                    .setMessage("Route unregistered successfully for device: " + deviceId)
                    .build();

            responseObserver.onNext(response);
            responseObserver.onCompleted();
        } catch (Exception e) {
            logger.error("Error unregistering route for device: " + deviceId, e);
            UnregisterDeviceGatewayRouteResponse errorResponse = UnregisterDeviceGatewayRouteResponse.newBuilder()
                    .setMessage("Error unregistering route: " + e.getMessage())
                    .build();
            responseObserver.onError(e);
        }
    }
}